package com.bwt.hashTable;

import java.util.Scanner;

public class HashTableDemo {
      public static void main(String[] args) {
        HashTable hashTable = new HashTable(7);
        String key = "";
        boolean loop = true;

        Scanner scanner = new Scanner(System.in);
        //输出一个菜单
        while (loop) {
            System.out.println("add: 添加雇员");
            System.out.println("list: 显示雇员");
            System.out.println("find: 查找雇员");
            System.out.println("exit: 退出程序");
            key = scanner.next();
            switch (key) {
                case "list":
                    hashTable.list();
                    break;
                case "add":
                    System.out.println("输出Id");
                    int id = scanner.nextInt();
                    System.out.println("输入名字");
                    String name = scanner.next();
                    Emp emp = new Emp(id, name);
                    hashTable.add(emp);
                    break;
                case "find":
                    System.out.println("请输入要查找的id");
                     id = scanner.nextInt();
                    hashTable.findById(id);
                    break;
                case "del":
                    System.out.println("请输入要删除的id");
                    id = scanner.nextInt();
                    hashTable.delById(id);
                    break;
                case "exit":
                    scanner.close();
                    loop = false;
                    break;
                default:
                    break;
            }
        }
        System.out.println("程序退出-------");
    }
}

//创建HashTable
class HashTable {
   private EmpLinkedList[] empLinkedListArray;
    private int size;

    public HashTable(int size) {
        this.size = size;
        this.empLinkedListArray = new EmpLinkedList[size];
        //这是不要忘记初始化每一条链表
        for (int i = 0; i < size; i++) {
            empLinkedListArray[i] = new EmpLinkedList();
        }
    }

    //添加雇员
    public void add(Emp emp) {
        //根据员工的id, 得到该员工应当添加到哪条链表
        int empLinkedListNO = hashFun(emp.id);
        empLinkedListArray[empLinkedListNO].add(emp);
    }

    //编写一个散列函数, 使用简单取模法
    public int hashFun(int id) {
        return id % size;
    }

    //遍历所有的链表
    public void list() {
        for (int i = 0; i < size; i++) {
            empLinkedListArray[i].list(i);
        }
    }
    public void findById(int id) {
        int no = hashFun(id);
        Emp emp = empLinkedListArray[no].findById(id);
        if (emp != null) {
            System.out.println("第"+no+"链表中找到该雇员, 信息为:"+emp);
        } else {
            System.out.println("在hash表中没有找到该雇员~");
        }
    }

    public void delById(int id) {
        int no = hashFun(id);
        empLinkedListArray[no].delById(id);
    }
}

//表示一个雇员
class Emp {
    public int id;
    public String name;
    public Emp next;

    public Emp(int id, String name) {
        this.id = id;
        this.name = name;
    }

    @Override
    public String toString() {
        return "Emp{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}

//创建EmpLinkedList , 表示链表
class EmpLinkedList {
    //头指针,指向第一个emp,因此这个链表的head 是直接指向第一个emp
    private Emp head;//默认为null


    /**
     * 添加雇员到链表
     * 说明
     * 1. 假定, 当添加雇员时,id是自增的
     * 2. 因此我们将该雇员直接加入到本链表的最后即可
     * @param emp
     */
    public void add(Emp emp) {
        if (head==null) {
            head = emp;
            return;
        }
        //如果不是第一个雇员, 则使用辅助指针,帮助定位到最后
        Emp temp = this.head;
        while (temp.next != null) {
            temp = temp.next;
        }
        temp.next = emp;
    }
    //遍历链表的雇员信息
    public void list(int no) {
        if (head == null) {
            System.out.println("第"+no+"链表为空");
            return;
        }
        System.out.print("第"+no+"链表的信息为:");
        Emp temp = this.head;
        while (temp != null) {
            System.out.print(temp);
            temp = temp.next;
        }
        System.out.println();
    }
    //根据id查找雇员
    // 查找到就返回emp,没有找到就返回null
    public Emp findById(int id) {
        if (head == null) {
            System.out.println("链表为空");
            return null;
        }
        Emp temp = this.head;
        while (temp != null && temp.id != id) {
            temp = temp.next;
        }
        return temp;
    }

    public void delById(int id) {
        if (head == null) {
            System.out.println("链表为空");
            return ;
        }
        if (head.id == id) {
            head = head.next;
            System.out.println("Id为" + id + "的员工已经被删除~~");
            return;
        }
        Emp temp = this.head;
        while (temp.next != null && temp.next.id != id) {
            temp = temp.next;
        }
        if (temp.next != null && temp.next.id == id) {
            temp.next = temp.next.next;
            System.out.println("Id为" + id + "的员工已经被删除~~");
        } else {
            System.out.println("没有这个员工！！！");
        }
    }
}